package com.mall.service.marketing.controller;

import com.mall.commons.common.CommonResult;
import com.mall.model.domain.SmsHomeNewProduct;
import com.mall.model.vo.PageResult;
import com.mall.service.marketing.service.SmsHomeNewProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/home/newProduct")
public class SmsHomeNewProductController {

    @Autowired
    private SmsHomeNewProductService smsHomeNewProductService;

    /**
     * 分页查询首页新品
     * @param productName
     * @param recommendStatus
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public CommonResult list( String productName, Integer recommendStatus,
                              @RequestParam(defaultValue = "5") Integer pageSize,
                              @RequestParam(defaultValue = "1") Integer pageNum){
        PageResult pr = smsHomeNewProductService.findList(productName,recommendStatus,pageNum,pageSize);
        return CommonResult.success(pr);

    }

    /**
     * 添加首页新品
     * @param smsHomeNewProduct
     * @return
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody SmsHomeNewProduct smsHomeNewProduct){
        smsHomeNewProductService.save(smsHomeNewProduct);
        return CommonResult.success("添加成功");
    }

    /**
     * 批量删除首页新品
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public CommonResult delete(@RequestParam List<Integer> ids){
        smsHomeNewProductService.removeByIds(ids);
        return CommonResult.success("删除成功");
    }

    /**
     *批量修改首页新品状态
     * @param ids
     * @param recommendStatus
     * @return
     */
    @PostMapping("/update/recommendStatus")
    public CommonResult update(@RequestParam List<Integer> ids,Integer recommendStatus){
        for (Integer id : ids) {
            SmsHomeNewProduct smsHomeNewProduct = smsHomeNewProductService.getById(id);
            smsHomeNewProduct.setRecommendStatus(recommendStatus);
            smsHomeNewProductService.updateById(smsHomeNewProduct);
        }
        return CommonResult.success("修改状态成功");
    }

    /**
     * 修改首页新品排序
     * @param id
     * @param sort
     * @return
     */
    @PostMapping("/update/sort/{id}")
    public CommonResult updateSort(@PathVariable Integer id,Integer sort){
        SmsHomeNewProduct smsHomeNewProduct = smsHomeNewProductService.getById(id);
        smsHomeNewProduct.setSort(sort);
        smsHomeNewProductService.updateById(smsHomeNewProduct);
        return CommonResult.success("修改排序成功");
    }
}
